﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class Topics_Topics_Charts_detail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd();
        }
    }

    void bd()
    {
        if (Request["flag"] == "edit")
        {
            long id = long.Parse(Request["id"]);
            CMSModel.CMSEntities cms = new CMSModel.CMSEntities();
            var c = cms.Topics2Charts.First(x => x.Id == id);
            txtTitle.Text = c.Title;
            bool ch = c.IsBigImg == null ? false : c.IsBigImg.Value;
            CheckBox1.Checked = ch;
            Image1.ImageUrl = "~/file/" + c.Path;
        }
     
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string pathFolder = Server.MapPath("~/file/topics/" + Request["tid"] + "/");
        string fileName = FileUpload1.FileName;
        string houzui = "";
        if (fileName.IndexOf(".") > -1)
        {
            houzui = fileName.Substring(fileName.IndexOf("."));
        }
        string guid = Guid.NewGuid().ToString();
        ViewState["filename"] = "/topics/" + Request["tid"] + "/" + guid + houzui;
        string rName = pathFolder + guid + houzui;
        FileInfo file = new FileInfo(rName);
        if (!file.Directory.Exists)
        {
            file.Directory.Create();
        }
        FileUpload1.SaveAs(rName);
        Image1.ImageUrl = "~/file/topics/" + Request["tid"] + "/" + guid + houzui;
    }
    protected void btnReturn_Click(object sender, EventArgs e)
    {
        Response.Redirect("Topics_Charts.aspx?tid=" + Request["tid"]);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        CMSModel.CMSEntities cms = new CMSModel.CMSEntities();
        CMSModel.Topics2Charts charts = null;
        if (string.IsNullOrEmpty(Request["id"]))
        {
            charts = new CMSModel.Topics2Charts();
        }
        else
        {
            long id=long.Parse(Request["id"]);
            charts = cms.Topics2Charts.First(x => x.Id == id);
        }
        charts.IsBigImg = CheckBox1.Checked;
        if(ViewState["filename"]!=null)
        {
            charts.Path = ViewState["filename"].ToString();
        }
        charts.Title = txtTitle.Text;
        if (string.IsNullOrEmpty(Request["id"]))
        {
            charts.Tid = int.Parse(Request["tid"]);
            cms.Topics2Charts.AddObject(charts);
        }
        cms.SaveChanges();
        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('保存成功');document.location='Topics_Charts.aspx?tid=" + charts.Tid + "'", true);
    }
}